namespace ScrumSprintMonitor.TestbenchApp
{
    #region #using Directives

    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Windows;

    using Design;

    using Infrastructure;
    using Infrastructure.Configuration;
    using Infrastructure.IoC;

    using Microsoft.Practices.Unity;

    using Rhino.Mocks;

    using Testbench.Impl;

    using UI.Wpf;

    using Views.TestbenchView;

    #endregion

    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses",
        Justification = "The UnityContainerAbstraction will create this class")]
    internal class UnityAppDesignTimeModule : StandardModule
    {
        /// <summary>
        ///   Loads the module into the container.
        /// </summary>
        public override void Load()
        {
            SetupDesignTimeBindings();
        }

        private void SetupDesignTimeBindings()
        {
            var testbenchViewModel = MockRepository.GenerateStub<ITestbenchViewModel>();
            testbenchViewModel.Stub(x => x.AvailableBuildStatus).Return(Enum.GetValues(typeof(BuildStatus)).Cast<BuildStatus>().ToList());
            testbenchViewModel.CurrentBuildStatus = BuildStatus.NotSetUp;
            testbenchViewModel.Stub(x => x.WorkItems).Return(WritableWorkItem.ConvertToBindingList(TestAssetLibrary.CreateWorkItems()));
            testbenchViewModel.Stub(x => x.ValidTeamMembers).Return(
                TestAssetLibrary.CreateTeamMemberInfoProvider().TeamMembers.Select(x => x.Name).ToList());
            Container.RegisterInstance(testbenchViewModel);

            var shellLayoutConfiguration = new ShellLayoutConfiguration(false, true, true, SprintMode.Sprinting);
            Container.RegisterInstance<IShellLayoutConfiguration>(shellLayoutConfiguration);
        }
    }

    /// <summary>
    ///   Provides a custom implementation of DesignerProperties.GetIsInDesignMode
    ///   to work around an issue.
    /// </summary>
    internal static class StubDesignerProperties
    {
        /// <summary>
        ///   Stores the computed InDesignMode value.
        /// </summary>
        private static bool? isInDesignMode;

        /// <summary>
        ///   Returns whether the control is in design mode (running under Blend
        ///   or Visual Studio).
        /// </summary>
        /// <param name = "element">The element from which the property value is
        ///   read.</param>
        /// <returns>True if in design mode.</returns>
        [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "element",
            Justification =
                "Matching declaration of System.ComponentModel.DesignerProperties.GetIsInDesignMode (which has a bug and is not reliable).")]
        public static bool GetIsInDesignMode(DependencyObject element)
        {
            if (!isInDesignMode.HasValue)
            {
                isInDesignMode = (null == Application.Current) || !(Application.Current is App);
            }
            return isInDesignMode.Value;
        }
    }
}